package com.gdou.teaching.service.impl;

import com.gdou.teaching.Enum.FileCategoryEnum;
import com.gdou.teaching.Enum.ResultEnum;
import com.gdou.teaching.dataobject.ExperimentAnswer;
import com.gdou.teaching.dto.AnswerDTO;
import com.gdou.teaching.dto.FileDTO;
import com.gdou.teaching.exception.TeachingException;
import com.gdou.teaching.mapper.ExperimentAnswerMapper;
import com.gdou.teaching.service.AnswerService;
import com.gdou.teaching.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author bo
 * @date Created in 20:59 2019/11/5
 * @description
 **/
@Service
@Slf4j
public class AnswerServiceImpl implements AnswerService {
    @Autowired
    ExperimentAnswerMapper answerMapper;
    @Autowired
    FileService fileService;
    @Override
    public AnswerDTO detail(Integer experimentId) {
        ExperimentAnswer experimentAnswer = answerMapper.selectExperimentAnswerByExperimentId(experimentId);
        List<FileDTO> fileDTOList = fileService.selectFileByCategoryAndFileCategoryId(FileCategoryEnum.EXPERIMENT_ANSWER_FILE.getCode(), experimentId);
        if(experimentAnswer!=null){
            AnswerDTO answerDTO=new AnswerDTO();
            BeanUtils.copyProperties(experimentAnswer,answerDTO);
            answerDTO.setExperimentAnswerFileList(fileDTOList);
            return answerDTO;
        }
        return null;
    }

    @Override
    public AnswerDTO save(AnswerDTO answerDTO) {
        ExperimentAnswer experimentAnswer=new ExperimentAnswer();
        BeanUtils.copyProperties(answerDTO,experimentAnswer);
        //如果ExperimentAnswerId为空,则为添加操作
        if (experimentAnswer.getExperimentAnswerId()==null){
            answerMapper.addExperimentAnswer(experimentAnswer);
            if (experimentAnswer.getExperimentAnswerId()==null){
                log.error("保存实验答案,新增实验答案失败,experimentAnswer={}",experimentAnswer);
                throw new TeachingException(ResultEnum.PARAM_ERROR);
            }
            //回填实验答案Id
            answerDTO.setExperimentAnswerId(experimentAnswer.getExperimentAnswerId());
        }else{ //更新操作
            boolean flag = answerMapper.updateExperimentAnswer(experimentAnswer);
            if (!flag){
                log.error("保存实验答案,修改实验答案失败,experimentAnswer={}",experimentAnswer);
                throw new TeachingException(ResultEnum.PARAM_ERROR);
            }
        }
        return answerDTO;
    }

    //todo 现实现根据experimentId删除实验答案, 可修改为根据experimentAnswerId删除(待讨论)
    @Override
    public boolean invalid(Integer experimentId) {
        ExperimentAnswer experimentAnswer
                = answerMapper.selectExperimentAnswerByExperimentId(experimentId);
        if (experimentAnswer == null) {
            log.error("删除实验答案,该实验答案不存在或已被删除");
            throw new TeachingException(ResultEnum.ANSWER_NOT_EXIST);
        }
        //更新
        boolean flag = answerMapper.deleteExperimentAnswerByExperimentAnswerId(experimentAnswer.getExperimentAnswerId());
        if (!flag){
            log.error("删除实验答案,实验答案删除失败");
            throw new TeachingException(ResultEnum.ANSWER_INVALID_ERROR);
        }
        return true;
    }
}
